package com.example.picturestorage.controller;

import com.example.picturestorage.bean.Book;
import com.example.picturestorage.mapper.BookMapper;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;

import javax.servlet.http.HttpServletRequest;

@Controller
public class FormController {

    @Autowired
    private BookMapper bookMapper;

    @RequestMapping("/books")
    public String books() {
        return "allBook";
    }

    @GetMapping("/node_test")
    public String node_test() {
        return "node_test";
    }

    @GetMapping("/publisher/add")
    public String addPublisher() {
        return "publisher/add";
    }

    @GetMapping("/testJqueryGet")
    @ResponseBody
    public Book testJqueryGet(HttpServletRequest request, Model model) {
        int id = Integer.parseInt(request.getParameter("id"));
        Book b=new Book();
        b.setId(id);
        Book book = bookMapper.findBookById(b);
        model.addAttribute(book);
        System.out.println("testJqueryGet: " + book);
        return book;
    }

   /* @RequestMapping("/error")
    public String error(){
        return "error";
    }*/

    @RequestMapping("/error_test/id")
    @ResponseBody
    public String error_test(@PathVariable("id") int id) {
        if (id == 1)
            throw new RuntimeException();
        return "111";
    }

    @GetMapping("/myBeauty/{id}")
    public String myBeauty(@PathVariable("id") Integer id,Model model){
        model.addAttribute("id",id);
        return "myBeauty";
    }

    @GetMapping("/bookAddForm")
    public String addBookForm(){
        return "/addBook";
    }

    @GetMapping("publisherAddForm")
    public String publisherAddForm(){
        return "publisher/add";
    }
}
